<template>
	<div class="todo-footer">
		<label>
			<input type="checkbox" :checked="isAll" @change="handleCheckAll"/>
			<!-- <input type="checkbox" v-model="isAll"/> -->
		</label>
		<span> <span>已完成{{doneTotal}}</span> / 全部{{total}} </span>
		<button class="btn btn-danger" @click="handleClearAllDone">清除已完成任务</button>
	</div>
</template>

<script>
	import { mapState } from 'vuex'

	export default {
		name:'Footer',
		computed:{
			...mapState(['todos']),
			//总数
			total(){
				return this.todos.length
			},
			//完成的总数
			doneTotal(){
				//使用中转变量配合forEach去计算
				/* let n = 0
				this.todos.forEach(t => {
					if(t.done) n++
				});
				return n */

				//使用filter去计算
				/* const x = this.todos.filter((t)=>{
					return t.done
				})
				return x.length */

			  //使用reduce去计算
				return this.todos.reduce((pre,current)=> pre + (current.done ? 1 : 0) ,0)
			},
			/* isAll(){
				// 第一种写法：依赖其他两个计算属性去计算
				// return this.total === this.doneTotal

				// 第二种写法：使用数组的every方法去计算
				return this.todos.every(t => t.done)
			} */
			isAll(){
				return this.todos.every(t => t.done)
			}
		},
		methods: {
			handleCheckAll(e){
				const {checked} = e.target
				this.$store.commit('CHECK_ALL',checked)
			},
			handleClearAllDone(){
				this.$store.commit('CLEAR_ALL_DONE')
			}
		},
	}
</script>

<style scoped>
	/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>